import axios from "axios";

export type Version = 'v2.1.0+'|'v2.3.0+';
export interface VersionConfig {
  pageHost:string;
  serverHost: string;
  wsHost: string;
}
export type VersionMap = {
  [key in Version]:VersionConfig
};

// let versionMap: VersionMap;
// let currentVersion: Version;
// let versionConfig: VersionConfig&{
//   pluginDownloadPage: string;
// };

// export const initVersionInfo = async () => {
//   if (import.meta.env.VITE_EVN === 'development') {
//     const localVersions = await axios.get('/versionConfig-demo.json');
//     versionMap = localVersions.data;
//   }else{
//     const versions = await axios.get('/versionConfig.json');
//     versionMap = versions.data;
//   }
//   currentVersion = (function() {
//     const org = window.location.origin;
//     for (const v in versionMap) {
//       if ((versionMap as any)[v].pageHost.indexOf(org) > -1) {
//         return v as Version;
//       }
//     }
//     return 'v2.1.0+';
//   }());
//   versionConfig = {
//     pluginDownloadPage: 'https://git.chainmaker.org.cn/chainmaker/chainmaker-smartplugin/-/releases',
//     ...versionMap[currentVersion]
//   };
// }

// export const getVersionInfo = ()=>{
//   return {
//     versionMap,
//     currentVersion,
//     versionConfig
//   }
// }

export const versionMap: VersionMap = (window as any).appConfig.versionMap;
if(!versionMap){
  console.warn('缺少versonConfig.js配置，参考versionConfig-demo.js')
}

export const currentVersion: Version = (function() {
  const org = window.location.origin;
  for (const v in versionMap) {
    if ((versionMap as any)[v].pageHost.indexOf(org) > -1) {
      return v as Version;
    }
  }
  return 'v2.1.0+';
}());
export const versionConfig = {
  pluginDownloadPage: 'https://git.chainmaker.org.cn/chainmaker/chainmaker-smartplugin/-/releases',
  ...versionMap[currentVersion]
};
